home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / MACSHELL / MS1 / COMMANDS / HEXDUMP.C < prev    next >
Text File  |  1992-12-02  |  7KB  |  284 lines

  1. /*
  2.  *    MacShell Source File
  3.  *
  4.  *    Copyright (c) 1989, 1990, 1991, 1992  Suick Bay Technologies.  All rights reserved.
  5.  *
  6.  *
  7.  *    RESTRICTIONS ON MacShell program and source code.
  8.  *
  9.  *    Ñ╩MacShell¬ is a product of Suick Bay Technologies and is provided for
  10.  *    restricted use by the owner of the CDROM "Disk to the future II".
  11.  *
  12.  *    Ñ╩No permission is granted for any commercial use without the written
  13.  *    consent of the Suick Bay Technologies.
  14.  *
  15.  *    Ñ╩No permission is granted for any redistribution of any kind use without
  16.  *    the written consent of the Suick Bay Technologies.
  17.  *
  18.  *    Ñ╩Permission is granted to use this for any personal noncommercial use.
  19.  *
  20.  *    Ñ╩You may not distribute source or executable code at all, nor may you 
  21.  *    distribute it with or within a commercial product without the written
  22.  *    consent of the Suick Bay Technologies.  Please send modifications to 
  23.  *    the author for inclusion in updates to the program.  Thanks.
  24.  *
  25.  *
  26.  *    MacShell¬ IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  27.  *    WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  28.  *    PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  29.  *
  30.  *    SUICK BAY TECHNOLOGIES SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  31.  *    INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY MACSHELL
  32.  *    OR ANY PART THEREOF. 
  33.  *
  34.  *    In no event will Suick Bay Technologies be liable for any lost revenue
  35.  *    or profits or other special, indirect and consequential damages, even if
  36.  *    Suick Bay Technologies has been advised of the possibility of such damages.
  37.  *
  38.  *    Suick Bay Technologies can be reached at:
  39.  *    
  40.  *    8768 Cottonwood lane
  41.  *    Maple Grove, MN 55369
  42.  *    Voice: (612) 425-7025
  43.  *    AppleLink: D5233
  44.  *    
  45.  *
  46.  *    No parts of this software may be reproduced or stored in a
  47.  *    retrieval system or transmitted in any form, or any means,
  48.  *    electronic, mechanical, photocopying, recording or otherwise,
  49.  *    without the prior written permission of Suick Bay Technologies.
  50.  *    
  51.  *    Spread the word and not the disk.
  52.  *    
  53.  *    SPK 030190    :    Fixed file dumps that eof with 0 count
  54.  *    SPK 022890    :    Fixed hex format and added file name printhexData
  55.  *    SPK 012290    :    Initial
  56.  */
  57.  
  58. #include    "SystemPub.h"
  59. #include    "Proc.h"
  60. #include    "ShellPub.h"
  61. #include    "Path.h"
  62.  
  63. #define        BUFSIZE        256
  64.  
  65. /*******************************************************************
  66.  *
  67.  *    Function HD
  68.  *
  69.  *    Dumps files
  70.  *
  71.  *    PathName Callback function
  72.  *
  73.  *    usage HD [options] [names]    
  74.  *
  75.  *******************************************************************/
  76.             
  77. #define        HDHex        (**MyShell).Proc[ProcID].bflags.f0
  78. #define        HDInt        (**MyShell).Proc[ProcID].bflags.f1
  79. #define        HDChar        (**MyShell).Proc[ProcID].bflags.f2
  80. #define        HDResFork    (**MyShell).Proc[ProcID].bflags.f3
  81. #define        HDAbort        (**MyShell).Proc[ProcID].bflags.f4
  82.             
  83. void        DoDumpLine( WHandle ShellWh, int16 ProcID, int32 index,
  84.                 int32 count, char *info )
  85. {
  86. int32        *lptr;
  87. int16        *iptr, i;
  88. int16        c;
  89. char        buf[ 64 ], asciiData[ 64 ], hexData[ 64 ];
  90. ShellWindRec    **MyShell = (ShellWindRec **) (**ShellWh).thing;
  91.  
  92.     *asciiData = '\0';
  93.     *hexData = '\0';
  94.         
  95.     if( HDInt )
  96.         {
  97.         iptr = (int16 *) info;
  98.         
  99.         for( i = 0; i < count/2; i++ )
  100.             {
  101.             if( HDHex )
  102.                 sprintf( buf, "%04X ", *iptr );
  103.             else
  104.                 sprintf( buf, "%06d ", *iptr );
  105.                 
  106.             strcat( hexData, buf );
  107.             *iptr++;
  108.             }
  109.         }
  110.     else
  111.         {
  112.         lptr = (int32 *) info;
  113.         
  114.         for( i = 0; i < count/4; i++ )
  115.             {
  116.             if( HDHex )
  117.                 sprintf( buf, "%08lX ", *lptr );
  118.             else
  119.                 sprintf( buf, "%012ld ", *lptr );
  120.                 
  121.             strcat( hexData, buf );
  122.             *iptr++;
  123.             }
  124.         }
  125.  
  126.     if( HDChar )
  127.         {
  128.         for( i = 0; i < count; i++ )
  129.             {
  130.             c = info[ i ];
  131.                     
  132.             if( !isPrint( c ))
  133.                 c = '.';
  134.                         
  135.             sprintf( buf, "%c", c );
  136.             strcat( asciiData, buf );
  137.             }
  138.         }
  139.                 
  140.     if( HDHex )
  141.         procPrintf( ShellWh, ProcID, "%8lX : %-40s  %s\n", index, hexData, asciiData );
  142.     else
  143.         procPrintf( ShellWh, ProcID, "%8lX : %-56s  %s\n", index, hexData, asciiData );
  144. }
  145.  
  146. /*******************************************************************/
  147.  
  148. void    HDCallBack( WHandle ShellWh, int16 ProcID, char *path, char *last,
  149.                         pathType what, int16 vRefNum, int32 dirID )
  150. {
  151. char    readBuf[ 64 ], str[ 256 ];
  152. int16    err, fileRefNum;
  153. int32    count, index;
  154. ShellWindRec    **MyShell = (ShellWindRec **) (**ShellWh).thing;
  155.  
  156.     if( what == pathIsFile )
  157.         {
  158.         strcpy( str, last );
  159.         CtoPstr( str );
  160.     
  161.         if( HDResFork )
  162.             err = HOpenRF( vRefNum, dirID, str, fsRdPerm, &fileRefNum );
  163.         else
  164.             err = HOpen( vRefNum, dirID, str, fsRdPerm, &fileRefNum );
  165.  
  166.         if( err )
  167.             procPrintf( ShellWh, ProcID, "hd : can't dump %s (%d)\n",
  168.                 last, err );
  169.         else
  170.             {
  171.             procPrintf( ShellWh, ProcID, "hd : %s\n", last );
  172.             
  173.             while( !err && !HDAbort )
  174.                 {
  175.                 if( UserAbort() )
  176.                     HDAbort = TRUE;
  177.                 
  178.                 err = GetFPos( fileRefNum, &index );
  179.                 count = 16L;
  180.                 err = FSRead( fileRefNum, &count, readBuf );
  181.             
  182.                 if( (!err || err == eofErr) && count > 0 )
  183.                     DoDumpLine( ShellWh, ProcID, index, count, readBuf );
  184.                 }
  185.     
  186.             if( fileRefNum )    /* if a file open, close it */
  187.                 FSClose( fileRefNum );
  188.             }
  189.         }
  190.     else
  191.         procPrintf( ShellWh, ProcID, "hd : %s is not a file.\n", last );
  192. }
  193.  
  194. /*******************************************************************/
  195.  
  196. void        HDFile( WHandle ShellWh, int16 ProcID, char *argument )
  197. {
  198. ShellWindRec    **MyShell;
  199. int16            m;
  200.  
  201.     MyShell = (ShellWindRec **) (**ShellWh).thing;
  202.  
  203.     m = ExpandPath( ShellWh, ProcID, argument, (ProcPtr) HDCallBack,
  204.             (**MyShell).pwdVRefNum, (**MyShell).pwdDirID );
  205.  
  206.     ResetShellPWD( ShellWh );
  207. }
  208.  
  209. /*******************************************************************/
  210.  
  211. Boolean            DoHD( int16 ProcToken, WHandle ShellWh, int16 ProcID, char *string )
  212. {
  213. int16            i, argc;
  214. char            *cp, argument[ 256 ];
  215. ShellWindRec    **MyShell = (ShellWindRec **) (**ShellWh).thing;
  216.  
  217.     switch( ProcToken )
  218.         {
  219.         case    PROC_INIT    :
  220.             (**MyShell).Proc[ ProcID ].flags = TRUE;
  221.             break;
  222.             
  223.         case    PROC_TERM    :
  224.         case    PROC_BREAK    :
  225.             HDAbort = TRUE;
  226.             /* Tell the shell that we're done */
  227.             SendOutToken( ShellWh, ProcID, PROC_BREAK );
  228.             /* Turn ourself off */
  229.             (**MyShell).Proc[ ProcID ].ProcActive = FALSE;
  230.             break;
  231.             
  232.         case    PROC_STDIN    :
  233.             if( (**MyShell).Proc[ ProcID ].flags )
  234.                 {
  235.                 (**MyShell).Proc[ ProcID ].flags = FALSE;        
  236.  
  237.                 HDHex     = TRUE;    /* else decimal */
  238.                 HDInt     = TRUE;    /* else longs    */
  239.                 HDChar     = TRUE;    /* else no chars added */
  240.                 HDResFork = FALSE;
  241.                 HDAbort    = FALSE;
  242.                 
  243.                 /* get arguments */
  244.                 argc = (**MyShell).Proc[ ProcID ].argc;
  245.                 for( i = 1; i < argc; i++ )
  246.                     {
  247.                     GetArgv( ShellWh, ProcID, i, argument );
  248.                     cp = argument;
  249.         
  250.                     if( *cp++ == '-' )
  251.                         while( *cp )
  252.                             switch( *cp++ )
  253.                                 {
  254.                                 case    'n'    :    /*  */
  255.                                     HDChar = FALSE;
  256.                                     break;
  257.                                 case    'l'    :    /*  */
  258.                                     HDInt = FALSE;
  259.                                     break;
  260.                                 case    'd'    :    /*  */
  261.                                     HDHex = FALSE;
  262.                                     break;
  263.                                 case    'r'    :    /*  */
  264.                                     HDResFork = TRUE;
  265.                                     break;
  266.                                 }
  267.                     }
  268.  
  269.                 for( i = 1; i < argc; i++ )
  270.                     {
  271.                     GetArgv( ShellWh, ProcID, i, argument );
  272.                     if( *argument != '-' )
  273.                         HDFile( ShellWh, ProcID, argument );
  274.                     }
  275.  
  276.                 /* Tell the shell that we're done */
  277.                 SendOutToken( ShellWh, ProcID, PROC_BREAK );
  278.                 /* Turn ourself off */
  279.                 (**MyShell).Proc[ ProcID ].ProcActive = FALSE;
  280.                 return( FALSE );
  281.                 }
  282.         }
  283. }
  284.